home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / DEMOS / GLIQ / PICK.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  3.8 KB  |  173 lines

  1. /*  
  2.  *  CS 453 - Final project : An OpenGL version of the pegboard game IQ
  3.  *  Due : June 5, 1997
  4.  *  Author : Kiri Wagstaff
  5.  *
  6.  * File : pick.c
  7.  * Description : Routines for picking ability.  MANY thanks to Nate
  8.  *                Robins since all of this code is his.  I couldn't
  9.  *                have done it without him. :)
  10.  *
  11.  */
  12.  
  13. #include <stdarg.h>
  14. #include "gliq.h"
  15.  
  16. int picked=0;         /* Which piece has been selected? */
  17. GLuint    select_buffer[SELECT_BUFFER];
  18. GLboolean selection = GL_FALSE;
  19.  
  20. GLuint pick(int x, int y);
  21. void passive(int x, int y);
  22. void text(GLuint x, GLuint y, GLfloat scale, char *format, ...);
  23.  
  24. GLuint pick(int x, int y)
  25. {
  26.   GLuint    i, hits, num_names, picked;
  27.   GLuint*   p;
  28.   GLboolean save;
  29.   GLuint    depth = (GLuint)-1;
  30.   GLint     viewport[4];
  31.   int height = glutGet(GLUT_WINDOW_HEIGHT);
  32.   int width = glutGet(GLUT_WINDOW_WIDTH);
  33.  
  34.   /* fill in the current viewport parameters */
  35.   viewport[0] = 0;
  36.   viewport[1] = 0;
  37.   viewport[2] = width;
  38.   viewport[3] = height;
  39.  
  40.   /* set the render mode to selection */
  41.   glRenderMode(GL_SELECT);
  42.   selection = GL_TRUE;
  43.   glInitNames();
  44.   glPushName(0);
  45.  
  46.   /* setup a picking matrix and render into selection buffer */
  47.   glMatrixMode(GL_PROJECTION);
  48.   glPushMatrix();
  49.  
  50.   glLoadIdentity();
  51.   gluPickMatrix(x, viewport[3] - y, 5.0, 5.0, viewport);
  52.   gluPerspective(60.0, (GLfloat)viewport[3]/(GLfloat)viewport[2], 1.0, 128.0);
  53.  
  54.   glMatrixMode(GL_MODELVIEW);
  55.   glLoadIdentity();
  56.   glTranslatef(0.0, -2.0, -15.0);
  57.  
  58.   switch(curstate)
  59.     {
  60.     case SELBOARD:
  61.       /* Draw the quit button */
  62.       drawquit(7.0, 9.0, 0.4, 1.0);
  63.       displaybuttons();
  64.       break;
  65.     case PLAY:
  66.       /* Draw the quit button */
  67.       drawquit(7.0, 9.0, 0.4, 1.0);
  68.       glPushMatrix();
  69.       glRotatef(45.0, 1.0, 0.0, 0.0);
  70.       tbMatrix();
  71.       drawpegs();
  72.       glPopMatrix();
  73.       break;
  74.     case HIGHSC:
  75.       break;
  76.     case VIEWSCORES:
  77.       break;
  78.     default:
  79.       printf("Unknown state %d, exiting.\n", curstate);
  80.       exit(1);
  81.     }
  82.  
  83.   glMatrixMode(GL_PROJECTION);
  84.   glPopMatrix();
  85.   glMatrixMode(GL_MODELVIEW);
  86.  
  87.   hits = glRenderMode(GL_RENDER);
  88.  
  89.   selection = GL_FALSE;
  90.  
  91.   p = select_buffer;
  92.   picked = 0;
  93.   for (i = 0; i < hits; i++) {
  94.     save = GL_FALSE;
  95.     num_names = *p;            /* number of names in this hit */
  96.     p++;
  97.  
  98.     if (*p <= depth) {            /* check the 1st depth value */
  99.       depth = *p;
  100.       save = GL_TRUE;
  101.     }
  102.     p++;
  103.     if (*p <= depth) {            /* check the 2nd depth value */
  104.       depth = *p;
  105.       save = GL_TRUE;
  106.     }
  107.     p++;
  108.  
  109.     if (save)
  110.       picked = *p;
  111.  
  112.     p += num_names;            /* skip over the rest of the names */
  113.   }
  114.  
  115.   return picked;
  116. }
  117.  
  118. void passive(int x, int y) 
  119. {
  120.   picked = pick(x,y);
  121.   glutPostRedisplay();
  122. }
  123.  
  124. /* text: general purpose text routine.  draws a string according to
  125.  * format in a stroke font at x, y after scaling it by the scale
  126.  * specified (scale is in window-space (lower-left origin) pixels).  
  127.  *
  128.  * x      - position in x (in window-space)
  129.  * y      - position in y (in window-space)
  130.  * scale  - scale in pixels
  131.  * format - as in printf()
  132.  */
  133. void 
  134. text(GLuint x, GLuint y, GLfloat scale, char* format, ...)
  135. {
  136.   va_list args;
  137.   char buffer[255], *p;
  138.   GLfloat font_scale = 119.05 + 33.33;
  139.  
  140.   va_start(args, format);
  141.   vsprintf(buffer, format, args);
  142.   va_end(args);
  143.  
  144.   glMatrixMode(GL_PROJECTION);
  145.   glPushMatrix();
  146.   glLoadIdentity();
  147.   gluOrtho2D(0, glutGet(GLUT_WINDOW_WIDTH), 0, glutGet(GLUT_WINDOW_HEIGHT));
  148.  
  149.   glMatrixMode(GL_MODELVIEW);
  150.   glPushMatrix();
  151.   glLoadIdentity();
  152.  
  153.   glPushAttrib(GL_ENABLE_BIT);
  154.   glDisable(GL_LIGHTING);
  155.   glDisable(GL_TEXTURE_2D);
  156.   glDisable(GL_DEPTH_TEST);
  157.   glTranslatef(x, y, 0.0);
  158.  
  159.   glScalef(scale/font_scale, scale/font_scale, scale/font_scale);
  160.  
  161.   for(p = buffer; *p; p++)
  162.     glutStrokeCharacter(GLUT_STROKE_ROMAN, *p);
  163.   
  164.   glPopAttrib();
  165.  
  166.   glPopMatrix();
  167.   glMatrixMode(GL_PROJECTION);
  168.   glPopMatrix();
  169.   glMatrixMode(GL_MODELVIEW);
  170. }
  171.  
  172.  
  173.